package org.japo.java.entities;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.FileInputStream;
import java.util.Properties;

/**
 *
 * @author José A. Pacheco Ondoño - joanpaon@gmail.com
 */
public class EnlaceBD {

    // Valores por defecto para la conexión a la base de datos
    public static final String TIPO = "MySQL";
    public static final String DRIVER = "com.mysql.jdbc.Driver";
    public static final String PROTOCOLO = "jdbc:mysql";
    public static final String SERVIDOR = "127.0.0.1";
    public static final String PUERTO = "3306";
    public static final String BD = "Agenda";
    public static final String USER = "usuario";
    public static final String PASS = "usuario";

    // Campos de la clase
    private String tipo;
    private String driver;
    private String protocolo;
    private String servidor;
    private String puerto;
    private String bd;
    private String user;
    private String pass;

    // Propiedades de la conexión
    private Properties propConexion = new Properties();
    
    // Anunciadores de propiedad
    public static final String PROP_TIPO = "tipo";
    public static final String PROP_DRIVER = "driver";
    public static final String PROP_PROTOCOLO = "protocolo";
    public static final String PROP_SERVIDOR = "servidor";
    public static final String PROP_PUERTO = "puerto";
    public static final String PROP_BD = "bd";
    public static final String PROP_USER = "user";
    public static final String PROP_PASS = "pass";

    // Soporte para el cambio de propiedad
    private transient final PropertyChangeSupport propertyChangeSupport
            = new PropertyChangeSupport(this);

    public EnlaceBD() {
        // Cargar parametros
        cargarParametros();
    }

    public EnlaceBD(String tipo, String clase, String protocolo,
            String servidor, String puerto, String bd,
            String usuario, String password) {
        this.tipo = tipo;
        this.driver = clase;
        this.protocolo = protocolo;
        this.servidor = servidor;
        this.puerto = puerto;
        this.bd = bd;
        this.user = usuario;
        this.pass = password;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        String oldTipo = this.tipo;
        this.tipo = tipo;
        propertyChangeSupport.firePropertyChange(PROP_TIPO, oldTipo, tipo);
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        String oldDriver = this.driver;
        this.driver = driver;
        propertyChangeSupport.firePropertyChange(PROP_DRIVER, oldDriver, driver);
    }

    public String getProtocolo() {
        return protocolo;
    }

    public void setProtocolo(String protocolo) {
        String oldProtocolo = this.protocolo;
        this.protocolo = protocolo;
        propertyChangeSupport.firePropertyChange(PROP_PROTOCOLO, oldProtocolo, protocolo);
    }

    public String getServidor() {
        return servidor;
    }

    public void setServidor(String servidor) {
        String oldServidor = this.servidor;
        this.servidor = servidor;
        propertyChangeSupport.firePropertyChange(PROP_SERVIDOR, oldServidor, servidor);
    }

    public String getPuerto() {
        return puerto;
    }

    public void setPuerto(String puerto) {
        String oldPuerto = this.puerto;
        this.puerto = puerto;
        propertyChangeSupport.firePropertyChange(PROP_PUERTO, oldPuerto, puerto);
    }

    public String getBd() {
        return bd;
    }

    public void setBd(String bd) {
        String oldBb = this.bd;
        this.bd = bd;
        propertyChangeSupport.firePropertyChange(PROP_BD, oldBb, bd);
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        String oldUser = this.user;
        this.user = user;
        propertyChangeSupport.firePropertyChange(PROP_USER, oldUser, user);
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        String oldPass = this.pass;
        this.pass = pass;
        propertyChangeSupport.firePropertyChange(PROP_PASS, oldPass, pass);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    private void cargarParametros() {
        // Carga los parámetros por defecto
        propConexion.setProperty("tipo", TIPO);
        propConexion.setProperty("driver", DRIVER);
        propConexion.setProperty("protocolo", PROTOCOLO);
        propConexion.setProperty("servidor", SERVIDOR);
        propConexion.setProperty("puerto", PUERTO);
        propConexion.setProperty("bd", BD);
        propConexion.setProperty("user", USER);
        propConexion.setProperty("pass", PASS);

        // Abre un fichero de propiedades
        
        try (FileInputStream in = new FileInputStream("conexion.properties")) {
            // Sobreescribe los parámetros encontrados
            propConexion.load(in);
        } catch (Exception ex) {
            System.out.println(ex);
        }

        // Establecer los valores de los campos
        tipo = propConexion.getProperty("tipo");
        driver = propConexion.getProperty("driver");
        protocolo = propConexion.getProperty("protocolo");
        servidor = propConexion.getProperty("servidor");
        puerto = propConexion.getProperty("puerto");
        bd = propConexion.getProperty("bd");
        user = propConexion.getProperty("user");
        pass = propConexion.getProperty("pass");
    }

    public Properties getPropConexion() {
        return propConexion;
    }

    public void setPropConexion(Properties propConexion) {
        this.propConexion = propConexion;
    }
}
